import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
const request = axios.create({
  baseURL: '/api'
})

request.interceptors.request.use((config) => {
  config.headers.Authorization = store.getters.token
  return config
}, err => {
  console.log(err)
  return Promise.reject(err)
})
request.interceptors.response.use((response) => {
  const { success, msg, token } = response.data
  // console.log(response)
  if (success) {
    // console.log(token)
    Message.success(msg)

    return token
  } else if (response.data.type === 'image/jpeg') {
    return response.data
  } else {
    Message.error(msg)
    // console.log(111)
    // return Promise.reject(new Error(msg))
    return Promise.reject(new Error(msg))
  }
  // console.log(response)
  // return response
}, err => {
  return Promise.reject(err)
})
export default request
